//
//  AppDelegate.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/19/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit
import JPFPSStatus
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        self.window = UIWindow(frame:UIScreen.mainScreen().bounds)
        self.window?.backgroundColor = UIColor.whiteColor()

        self.window?.rootViewController = setupRootVC()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(switchRootVC(_:)), name: FZSWITCHROOTVIEWCONTROLLER, object: nil)

        JPFPSStatus.sharedInstance().open()
        self.window?.makeKeyAndVisible()
        
        return true
    }

    func switchRootVC(noti: NSNotification) {
        
        self.window?.rootViewController = FZUserAccountViewModel.sharedUserAcount.isLogin ?
            (noti.object != nil ? FZMainViewController() :
                FZWelcomeViewController()) : FZMainViewController()

    }
    
    func setupRootVC() -> UIViewController {
        return isNewVersion() ? FZNewfeatureViewController () : FZUserAccountViewModel.sharedUserAcount.isLogin ? FZWelcomeViewController() : FZMainViewController()
    }
    
    //版本比较
    func isNewVersion() -> Bool {
        
        let curVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let curVersionID = Double(curVersion)!
        
        let oldVersionID = NSUserDefaults.standardUserDefaults().doubleForKey(FZAPPVERSION)
        
        NSUserDefaults.standardUserDefaults().setDouble(curVersionID, forKey: FZAPPVERSION)
        
        return curVersionID > oldVersionID
        
    }


}

